﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Shipping_Form : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_shipping_form model = new tbl_shipping_form();
            model.Work_Order_Number = txtWork_Order_Number.Text.Trim();
            model.Drum_Bag_Number = txtDrum_Bag_Number.Text.Trim();
            model.Actual_Quantity_from_QC_Sicomill = Convert.ToInt32(txtActual_Quantity_from_QC_Sicomill.Text.Trim());
            model.Shipping_Date = txtShipping_Date.Text.Trim().toDateTimeWithoutError();
            model.PO_Number = txtPO_Number.Text.Trim();
            model.Shipping_Method = ddlShipping_Method.SelectedValue;
            model.Shipping_Vessel = txtShipping_Vessel.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_shipping_form bll = new BLL_tbl_shipping_form();
            bll.insert(model);
        }
        else
        {
            tbl_shipping_form model = new tbl_shipping_form();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Work_Order_Number = txtWork_Order_Number.Text.Trim();
            model.Drum_Bag_Number = txtDrum_Bag_Number.Text.Trim();
            model.Actual_Quantity_from_QC_Sicomill = Convert.ToInt32(txtActual_Quantity_from_QC_Sicomill.Text.Trim());
            model.Shipping_Date = txtShipping_Date.Text.Trim().toDateTimeWithoutError();
            model.PO_Number = txtPO_Number.Text.Trim();
            model.Shipping_Method = ddlShipping_Method.SelectedValue;
            model.Shipping_Vessel = txtShipping_Vessel.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_shipping_form bll = new BLL_tbl_shipping_form();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_shipping_form model = new tbl_shipping_form();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_shipping_form bll = new BLL_tbl_shipping_form();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_shipping_form bll = new BLL_tbl_shipping_form();
                List<tbl_shipping_form> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_shipping_form tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    txtWork_Order_Number.Text = tbl.Work_Order_Number;
                    txtDrum_Bag_Number.Text = tbl.Drum_Bag_Number;
                    txtActual_Quantity_from_QC_Sicomill.Text = tbl.Actual_Quantity_from_QC_Sicomill.ToString();
                    txtShipping_Date.Text = tbl.Shipping_Date.ToString("yyyy-MM-dd");
                    txtPO_Number.Text = tbl.PO_Number;
                    ddlShipping_Method.SelectedValue = tbl.Shipping_Method;
                    txtShipping_Vessel.Text = tbl.Shipping_Vessel;
                    

                }
            }
        }
    }


}